package com.freight.tws.controller;

import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.freight.common.core.domain.R;
import lombok.RequiredArgsConstructor;
import com.freight.common.security.annotation.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.freight.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.freight.common.log.annotation.Log;
import com.freight.common.log.enums.BusinessType;
import com.freight.common.core.web.controller.BaseController;
import com.freight.common.core.web.domain.AjaxResult;
import com.freight.common.core.utils.poi.ExcelUtil;
import com.freight.common.core.web.page.Query;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freight.common.core.web.page.Condition;
import com.freight.tws.domain.OrderTrail;
import com.freight.tws.service.IOrderTrailService;
import com.freight.common.core.web.page.TableDataInfo;

/**
 * 订单手机定位轨迹Controller
 * 
 * @author ruoyi
 * @date 2023-02-28
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/trail" )
public class OrderTrailController extends BaseController
{
    private final IOrderTrailService iOrderTrailService;

    /**
     * 查询订单手机定位轨迹列表
     */
    @PreAuthorize(hasPermi="tws:trail:list")
    @GetMapping("/list")
    public TableDataInfo list(Query query, OrderTrail orderTrail)
    {
        LambdaQueryWrapper<OrderTrail> lqw = new LambdaQueryWrapper<OrderTrail>();
        if (orderTrail.getOrderId() != null) {
            lqw.eq(OrderTrail::getOrderId, orderTrail.getOrderId());
        }
        if (StringUtils.isNotBlank(orderTrail.getLongitude())) {
            lqw.eq(OrderTrail::getLongitude, orderTrail.getLongitude());
        }
        if (StringUtils.isNotBlank(orderTrail.getLatitude())) {
            lqw.eq(OrderTrail::getLatitude, orderTrail.getLatitude());
        }
        if (StringUtils.isNotBlank(orderTrail.getType())) {
            lqw.eq(OrderTrail::getType, orderTrail.getType());
        }
        if (StringUtils.isNotBlank(orderTrail.getUpCode())) {
            lqw.eq(OrderTrail::getUpCode, orderTrail.getUpCode());
        }
        lqw.orderByDesc(OrderTrail::getCreateTime);
        IPage<OrderTrail> list = iOrderTrailService.page(Condition.getPage(query), lqw);
        return getDataTable(list);
    }

    /**
     * 根据订单ID获取手机轨迹
     * @param orderId
     * @return
     */
    @GetMapping("/allTrail/{orderId}")
    public R<List<OrderTrail>> getAlltrail(@PathVariable("orderId") String orderId){
        LambdaQueryWrapper<OrderTrail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OrderTrail::getOrderId,orderId);
        List<OrderTrail> list = iOrderTrailService.list(lqw);
        return R.ok(list);
    }

    /**
     * 导出订单手机定位轨迹列表
     */
    @PreAuthorize(hasPermi = "tws:trail:export")
    @Log(title = "订单手机定位轨迹", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderTrail orderTrail) throws IOException
    {
        LambdaQueryWrapper<OrderTrail> lqw = new LambdaQueryWrapper<OrderTrail>(orderTrail);
        List<OrderTrail> list = iOrderTrailService.list(lqw);
        ExcelUtil<OrderTrail> util = new ExcelUtil<OrderTrail>(OrderTrail. class);
        util.exportExcel(response, list, "trail");
    }

    /**
     * 获取订单手机定位轨迹详细信息
     */
    @PreAuthorize(hasPermi="tws:trail:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iOrderTrailService.getById(id));
    }

    /**
     * 新增订单手机定位轨迹
     */
    @PreAuthorize(hasPermi="tws:trail:add")
    @Log(title = "订单手机定位轨迹", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderTrail orderTrail) {
        return toAjax(iOrderTrailService.save(orderTrail) ? 1 : 0);
    }

    /**
     * 修改订单手机定位轨迹
     */
    @PreAuthorize(hasPermi="tws:trail:edit")
    @Log(title = "订单手机定位轨迹" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderTrail orderTrail) {
        return toAjax(iOrderTrailService.updateById(orderTrail) ? 1 : 0);
    }

    /**
     * 删除订单手机定位轨迹
     */
    @PreAuthorize(hasPermi="tws:trail:remove")
    @Log(title = "订单手机定位轨迹", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iOrderTrailService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
